#include "Model.h"

Model::Model()
{
	clear();
}

Model::~Model()
{
}

int Model::getHType()
{
	return this->hType;
}

void Model::setHType(int type)
{
	this->hType = type;
}

int * Model::getStartPoint()
{
	return this->startPos;
}

int * Model::getEndPoint()
{
	return this->endPos;
}

int Model::getStartX()
{
	return this->startPos[0];
}

int Model::getStartY()
{
	return this->startPos[1];
}

int Model::getEndX()
{
	return this->endPos[0];
}

int Model::getEndY()
{
	return this->endPos[1];
}

int Model::getBlockType(int x, int y)
{
	return this->map[x][y];
}

void Model::setBlockType(int x, int y, int type)
{
	this->map[x][y] = type;
}

void Model::setStartPoint(int x, int y)
{
	if (x == -1 || y == -1) return;
	if (this->startPos[0] != -1 && this->startPos[1] != -1)
	{
		this->setBlockType(this->startPos[0], this->startPos[1], SPACE);
	}
	this->startPos[0] = x;
	this->startPos[1] = y;
	this->setBlockType(x, y, START_POINT);
}

void Model::setEndPoint(int x, int y)
{
	if (x == -1 || y == -1) return;
	if (this->endPos[0] != -1 && this->endPos[1] != -1)
	{
		this->setBlockType(this->endPos[0], this->endPos[1], SPACE);
	}
	this->endPos[0] = x;
	this->endPos[1] = y;
	this->setBlockType(x, y, END_POINT);
}

void Model::setBarrier(int x, int y)
{
	this->setBlockType(x, y, BARRIER);
}

void Model::setSpace(int x, int y)
{
	this->setBlockType(x, y, SPACE);
}

void Model::reset()
{
	for (int i = 0; i < MAP_BLOCK_COUNT; i++)
	{
		for (int j = 0; j < MAP_BLOCK_COUNT; j++)
		{
			if (map[i][j] == VISITED || map[i][j] == ON_PATH || map[i][j] == VISITING)
			{
				this->map[i][j] = SPACE;
			}
		}
	}
	if(startPos[0] != -1 && startPos[1] != -1)
		this->setBlockType(startPos[0], startPos[1], START_POINT);
	if (endPos[0] != -1 && endPos[1] != -1)
		this->setBlockType(endPos[0], endPos[1], END_POINT);
}

int * Model::tranform()
{
	int newMap[MAP_BLOCK_COUNT * MAP_BLOCK_COUNT];
	for (int j = 0; j < MAP_BLOCK_COUNT; j++)
	{
		for (int i = 0; i < MAP_BLOCK_COUNT; i++)
		{
			newMap[j * MAP_BLOCK_COUNT + i] = map[j][i];
		}
	}
	return newMap;
}

void Model::getBarriers(int list[], int & n)
{
	int k = 0;
	for (int i = 0; i < MAP_BLOCK_COUNT; i++)
	{
		for (int j = 0; j < MAP_BLOCK_COUNT; j++)
		{
			if (map[i][j] == BARRIER)
			{
				list[k++] = i;
				list[k++] = j;
			}
		}
	}
	n = k;
}

bool Model::check()
{
	return startPos[0] >= 0 && startPos[0] < MAP_BLOCK_COUNT \
		&& startPos[1] >= 0 && startPos[1] < MAP_BLOCK_COUNT \
		&& endPos[0] >= 0 && endPos[0] < MAP_BLOCK_COUNT \
		&& endPos[1] >= 0 && endPos[1] < MAP_BLOCK_COUNT;
}

void Model::clear()
{
	for (int i = 0; i < MAP_BLOCK_COUNT; i++)
	{
		for (int j = 0; j < MAP_BLOCK_COUNT; j++)
		{
			this->map[i][j] = SPACE;
		}
	}
	this->startPos[0] = -1;
	this->startPos[1] = -1;
	this->endPos[0] = -1;
	this->endPos[1] = -1;
	this->hType = 0;
}
